<template>
	<view>
		<!-- <u-navbar :is-back="true" :is-fixed="true" title="基础信息"></u-navbar> -->
		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 50px);" :show-scrollbar="false">
			<view class="pb-15">
				<free-divider></free-divider>
				<free-list-item title="营业执照" :showRight="false" :showLeftIcon="false" @click="update">
					<!-- <free-avater slot="right" :src="getFullImageUrl(newList.businessLicense)" size="100"
						style="width: 100px; height: 80px;"></free-avater> -->

					<u-image slot="right" :src="getFullImageUrl(newList.businessLicense)"
						style="width: 65px; height: 80px;" @error="handleImageError" mode="widthFix"></u-image>
				</free-list-item>
				<u-field :disabled="!edit" v-model="newList.enterpriseName" label="企业名称" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-field v-if="!edit" v-model="newList.phone" label="手机号" :disabled="true"
					input-align="right"></u-field>

				<u-field :disabled="!edit" v-model="newList.unifiedCreditCode" label="统一社会信用代码" label-width="230"
					input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="newList.registeredCapital" label="注册资本" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="newList.type" label="类型" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="newList.legalRepresentative" label="法定代表人" label-width="180"
					input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>

				<!-- 			<u-field v-model="newList.foundedTime" label="成立日期" input-align="right"
					:field-style="placeholderStyle"></u-field> -->
				<u-field :disabled="true" :right-icon="edit?'arrow-down-fill':''" @click="showFoundedTimeHandler"
					v-model="newList.foundedTime" label="成立日期" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-picker mode="time" @confirm="selectFoundedTimeTerm" :default-time="newList.foundedTime"
					v-model="foundedTimeShow" :params="params"></u-picker>

				<!-- <u-field v-model="newList.businessTerm" label="营业期限" input-align="right"
					:field-style="placeholderStyle"></u-field> -->
				<template v-if="!edit">
					<u-field :disabled="true" v-model="newList.businessTerm" label="营业期限" input-align="right"
						:field-style="edit?placeholderStyle:{}"></u-field>
				</template>
				<template v-else>
					<u-field :disabled="true" @click="showBusinessStartTermHandler"
						:right-icon="edit?'arrow-down-fill':''" v-model="newList.businessStartTerm" label="营业期限开始时间"
						input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>
					<u-picker mode="time" @confirm="selectBusinessStartTerm" :default-time="newList.businessStartTerm"
						v-model="businessStartTermShow" :params="params"></u-picker>

					<u-field :disabled="true" @click="showBusinessEndTermHandler"
						:right-icon="edit?'arrow-down-fill':''" v-model="newList.businessEndTerm" label="营业期限结束时间"
						input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>
					<u-picker mode="time" @confirm="selectBusinessEndTerm" :default-time="newList.businessEndTerm"
						v-model="businessEndTermShow" :params="params"></u-picker>
				</template>

				<!-- <u-field v-model="newList.address" label="主体地址" input-align="right"
					:field-style="placeholderStyle"></u-field> -->
				<u-field :disabled="!edit" v-model="newList.address" label="主体地址" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

				<!-- <u-field v-model="newList.natureBusiness" label="经营范围" input-align="right"
					:field-style="placeholderStyle"></u-field> -->
				<u-field :disabled="true" @click="showNatureBusinessHandler" :right-icon="edit?'arrow-down-fill':''"
					v-model="newList.natureBusiness" label="经营范围" input-align="right"
					:field-style="edit?placeholderStyle:{}">
				</u-field>
				<u-picker @confirm="selectNatureBusiness" mode="selector" v-model="showNatureBusiness"
					:default-selector="[0]" :range="natureBusinessList" range-key="text"></u-picker>

				<!-- <u-field v-model="newList.createTime" label="注册时间" input-align="right"
					:field-style="placeholderStyle"></u-field> -->
				<u-field :disabled="!edit" v-if="!edit" v-model="newList.createTime" label="注册时间" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

			</view>
			<ferrButton v-if="userInfo.parentId === null" :title="edit?'保存':'编辑信息'" @click="editAndSave"></ferrButton>
			<!-- <ferrButton v-else :title="edit?'保存':'编辑信息'" ></ferrButton> -->
		</scroll-view>

	</view>
</template>

<script>
	import $H from '@/common/free-lib/request.js';
	import {
		getImageUrl
	} from '@/common/free-lib/util.js';
	import freeListItem from "@/components/free-ui/free-list-item.vue";
	import freeAvater from "@/components/free-ui/free-avater.vue";
	import freeDivider from "@/components/free-ui/free-divider.vue";
	import ferrButton from "@/components/free-ui/ferr-button.vue";
	export default {
		components: {
			freeListItem,
			freeAvater,
			freeDivider,
			ferrButton,
		},
		data() {
			return {
				edit: false,
				showNatureBusiness: false,
				businessStartTermShow: false,
				businessEndTermShow: false,
				foundedTimeShow: false,
				newList: {
					address: "",
					businessEndTerm: "",
					businessLicense: "",
					businessStartTerm: "",
					buyScope: "",
					enterpriseName: "",
					foundedTime: "",
					id: "",
					legalRepresentative: "",
					natureBusiness: "",
					password: "",
					phone: "",
					phoneCaptcha: "",
					registeredCapital: "",
					state: "",
					type: "",
					unifiedCreditCode: "",
					userType: "",
					username: ""
				},
				placeholderStyle: {
					color: '#999'
				},
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				natureBusinessList: [{
						"text": "科技类",
						"value": "1"
					},
					{
						"text": "文化、传媒广告类",
						"value": "2"
					},
					{
						"text": "咨询类",
						"value": "3"
					},
					{
						"text": "管理类",
						"value": "4"
					},
					{
						"text": "商贸类",
						"value": "5"
					}
				],
				userInfo:null
			}
		},
		computed: {
           
		},
		onShow() {
			const userInfo = uni.getStorageSync('userInfo');
			this.userInfo = userInfo
			this.NewUser(userInfo)
		},
		methods: {
			handleImageError() {
			      uni.showToast({
			        title: '图片格式不支持jpeg，请重新上传',
			        icon: 'none',
			        duration: 4000
			      });
			     
			    },
			// 拼接图片方法
			getFullImageUrl(imagePath) {
				return getImageUrl(imagePath);
			},
			showNatureBusinessHandler() {
				if (!this.edit) {
					return;
				}
				this.showNatureBusiness = true;
			},
			showBusinessStartTermHandler() {
				if (!this.edit) {
					return;
				}
				this.businessStartTermShow = true;
			},
			showBusinessEndTermHandler() {
				if (!this.edit) {
					return;
				}
				this.businessEndTermShow = true;
			},
			showFoundedTimeHandler() {
				if (!this.edit) {
					return;
				}
				this.foundedTimeShow = true;
			},
			fullTime(time) {
				if (!time) {
					return "";
				}
				return time + " 00:00:00";
			},
			selectNatureBusiness(e) {
				// this.newList.natureBusinessText = this.natureBusinessList[e[0]].text;
				this.newList.natureBusiness = this.natureBusinessList[e[0]].text;
			},
			selectBusinessStartTerm(e) {
				this.newList.businessStartTerm = e.year + '-' + e.month + '-' + e.day;
				this.businessStartTermShow = false; // 关闭日期选择框
			},
			selectBusinessEndTerm(e) {
				this.newList.businessEndTerm = e.year + '-' + e.month + '-' + e.day;
				this.businessEndTermShow = false; // 关闭日期选择框
			},
			selectFoundedTimeTerm(e) {
				this.newList.foundedTime = e.year + '-' + e.month + '-' + e.day;
				this.foundedTimeShow = false;
			},
			NewUser(userInfo) {
				$H.get('/brand/user/queryById', {
					id: userInfo.id
				}, {}).then(res => {
					this.newList = res.result
				}).catch(error => {
					uni.showToast({
						title: "网络错误，请稍后再试",
						icon: 'none'
					});
				});
			},
			update() {
				if (!this.edit) {
					return;
				}
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					success: (res) => {
						// 上传
						this.avatar(res.tempFilePaths[0]);
					}
				})
			},
			avatar(filePath) {
				// 上传图片
				$H.uploadLicense('/sys/common/businessLicense/upload', {
					filePath: filePath,
				}).then((res) => {
					// 上传成功后，将图片地址存储在 this.ifdUser.avatar 中
					this.newList.businessLicense = res.message;

					if (res.result) {
						// If res.result is not null, proceed to update newList with business license details
						const result = res.result;
						this.newList.enterpriseName = result.name;
						this.newList.unifiedCreditCode = result.registration_number;
						this.newList.registeredCapital = result.registered_capital;
						this.newList.legalRepresentative = result.legal_representative;
						this.newList.foundedTime = result.found_date.replace(/[^\d]/g, '-').slice(0, -1);
						this.newList.address = result.address;
						// Update other fields accordingly
					} else {
						// If res.result is null, it indicates that the request for correct business license details failed
						uni.showToast({
							title: '请上传正确的营业执照',
							icon: 'none',
							duration: 4000
						});
						// Clear the uploaded image as it is invalid
						this.newList.businessLicense = null;
					}
				}).catch((error) => {
					console.error('上传失败', error);
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					});
				});
			},


			/**OCR识别 end**/


			editAndSave(id) {
				if (!this.edit) {
					this.edit = true;
					return;
				}
				let o = {
					...this.newList,
					id: this.newList.id,
				}
				const that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认修改',
					success: function(res) {
						if (res.confirm) {
							//修改用戶信息
							uni.showLoading({
								title: '加载中'
							});
							$H.post("/brand/user/agriculture/edit", o, {}).then(res => {
								uni.hideLoading();
								if (res.success) {
									uni.showToast({
										title: res.message,
										icon: "none",
									});
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										});
									}, 1000);
								} else {
									uni.showToast({
										title: res.message,
										icon: "none",
									});
								}
							}).catch(error => {
								console.error('请求出错', error); // 处理任何错误
								uni.showToast({
									title: "修改错误",
									icon: "none",
								});
								uni.hideLoading();
							})
						}
					}
				});
			},

			openCode() {
				uni.navigateTo({
					url: '/pages/my/code/code'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-border-bottom:after {
		border-bottom: 2px solid #dee2e6 !important;

	}
</style>